<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
    >
      <el-form-item label="患者来源" prop="source">
        <el-select v-model="queryParams.source" placeholder="请选择患者来源" clearable>
          <el-option
            v-for="dict in dict.type.patient_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="病种" prop="disease">
        <el-select v-model="queryParams.disease" placeholder="请选择患者所在病种" clearable>
          <el-option
            v-for="item in diseaseList"
            :key="item.id"
            :label="item.diseaseName"
            :value="item.diseaseName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="科室" prop="department">
        <el-select v-model="queryParams.department" placeholder="请选择患者所在科室" clearable>
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.departmentName"
            :value="item.departmentName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="病区" prop="ward">
        <el-select v-model="queryParams.ward" placeholder="请选择患者所在病区" clearable>
          <el-option
            v-for="item in wardList"
            :key="item.id"
            :label="item.ward"
            :value="item.ward"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="clinicList" @selection-change="handleSelectionChange" :default-sort = "{prop: 'clinicId', order: 'descending'}">
      <el-table-column label="序号" type="index" align="center" width="50"/>
      <el-table-column label="姓名" align="center" prop="patientName">
        <template slot-scope="scope">
          <router-link :to="'/patient/detail/' + scope.row.patientId" class="link-type">
            <span>{{ scope.row.patientName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="patientGender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.patientGender"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="patientAge"/>
      <el-table-column label="身份证号" align="center" prop="patientIdentityNo" width="200"/>
      <el-table-column label="科室" align="center" prop="department"/>
      <el-table-column label="病区" align="center" prop="ward"/>
      <el-table-column label="病种" align="center" prop="disease"/>
      <el-table-column label="患者来源" align="center" prop="source">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.patient_source" :value="scope.row.source"/>
        </template>
      </el-table-column>
      source
      <el-table-column style="min-width: 50px" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="'/workflow/task/schedule/' + scope.row.clinicId" class="link-type">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
            >分配
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listClinic, getClinic, delClinic, addClinic, updateClinic, listClinicbyone} from "@/api/gaoxin/clinic";
import IconsDialog from "@/views/tool/build/IconsDialog";
import {listPatient, getPatient, insertPatient, updatePatient} from "@/api/gaoxin/patient";
import {getUserAll} from "@/api/system/user";
import {listTaskalive} from "@/api/gaoxin/taskdata";
import dayjs from "@/api/gaoxin/dayjs.min";
import Recorder from "js-audio-recorder";
import {listDisease} from "@/api/system/disease";
import {listDepartment} from "@/api/system/department";
import {listWard} from "@/api/system/ward";

export default {
  name: "Clinic",
  components: {IconsDialog},
  dicts: ['disease','marital_status', 'clinic_status', 'patient_source', 'patient_status', 'department', 'ward', 'sys_user_sex'],
  data() {
    return {
      vloading: false,
      ocrsen: '',
      asrsen: '',
      recorder: null,
      playTime: 0,
      timer: null,
      recorderFlag: false,
      open0: false,
      open1: false,
      userOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者总条数
      ptotal: 0,
      // 就诊信息表格数据
      clinicList: [],
      // 患者列表信息表格数据
      patientList: [],
      // 弹出层标题
      title: "",
      // 患者列表弹出层标题
      patientTitle: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      vis: false,
      // pane
      activeName: "1",
      //患者下的任务是否都已审核
      isActive:true,
      //病种列表
      diseaseList: [],
      //科室列表
      departmentList: [],
      //病区列表
      wardList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        source: null,
        manageId: null,
        disease: null,
        department: null,
        ward: null,
        bedNum: null,
        createTime: null,
        outTime: null,
        patientId: null,
        patientName:null,
        status: null
      },
      // 查询参数
      queryPatientParams: {
        pageNum: 1,
        pageSize: 10,
        patientNo: null,
        patientName: null,
        patientBirthday: null,
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: null,
      },
      Form: {},
      // 表单参数
      form: {},
      // 表单参数
      patientForm: {
        patientBirthday: ''
      },
    };
  },
  created() {
    this.getList();
    // this.getListbyone();
    // console.log("------",this.$route);
    // console.log("------");
    this.getDiseaseList();
    this.getDepartmentList();
    this.getWardList();
  },
  methods: {
    //查询所有病种
    getDiseaseList() {
      this.loading = true;
      listDisease({diseaseName: null}).then(response => {
        this.diseaseList = response.rows;
        this.loading = false;
      });
    },
    //查询所有科室
    getDepartmentList() {
      this.loading = true;
      listDepartment({departmentName: null}).then(response => {
        this.departmentList = response.rows;
        this.loading = false;
      });
    },
    //查询所有病区
    getWardList() {
      this.loading = true;
      listWard({ward: null}).then(response => {
        this.wardList = response.rows;
        this.loading = false;
      });
    },
    /** 查询就诊信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.status = '0'; // 未归档
      listClinic(this.queryParams).then(response => {
        this.clinicList = response.rows;
        // console.log('cliniclist:',this.clinicList)
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询患者列表 */
    getPatientList() {
      this.loading = true;
      this.vis = true;
      this.patientTitle = '患者列表';
      // this.queryPatientParams.patientIdentityNo = null;
      listPatient(this.queryPatientParams).then(response => {
        this.patientList = response.rows;
        this.ptotal = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.activeName = "1";
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clinicId: null,
        source: null,
        preDiagnose: null,
        manageId: null,
        firstDoctorId: null,
        secondDoctorId: null,
        illnessSummary: null,
        preHistory: null,
        familyHistory: null,
        disease: null,
        department: null,
        ward: null,
        bedNum: null,
        createTime: null,
        outTime: null,
        patientId: null,
        patientName:null,
        status: "0"
      };
      this.resetForm("form");
    },
    patientReset() {
      this.patientForm = {
        patientId: null,
        patientNo: null,
        patientName: null,
        patientBirthday: '',
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: null,
      };
      this.resetForm("patientForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    patientQuery() {
      this.queryPatientParams.pageNum = 1;
      this.getPatientList();
    },
    /** 重置按钮操作 */
    patientResetQuery() {
      this.resetForm("queryPatientParams");
      this.patientQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clinicId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.patientReset();
      this.title = "添加就诊信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.patientReset();
      const clinicId = row.clinicId || this.ids
      getClinic(clinicId).then(response => {
        this.form = response.data;
        const patientId = this.form.patientId;
        this.open = true;
        this.title = "修改就诊信息";
        getPatient(patientId).then(response => {
          this.patientForm = response.data;
        });
      });

    },
    handleSelect(row) {
      this.vis = false;
      this.form.patientId = row.patientId;
      getPatient(row.patientId).then(response => {
        this.patientForm = response.data;
        // console.log(response.data)
      })
    },
    assignment(id) {
      this.form.patientId = id;
    },
    handleClick(tab, event) {
      // console.log(tab, event);
    },
    charge1() {
      this.activeName="1";
    },
    charge2() {
      this.activeName="2";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["patientForm"].validate(valid => {
        if(valid){
          this.$refs["form"].validate(valid => {
            if (valid) {
              if(this.patientForm.patientNo == null) {
                const identityNo = this.patientForm.patientIdentityNo;
                this.queryPatientParams.patientIdentityNo = identityNo;
                listPatient(this.queryPatientParams).then(response => {
                  this.patientList = response.rows;
                  this.ptotal = response.total;
                });
                if(this.ptotal === 0) {
                  insertPatient(this.patientForm).then(response => {
                    this.$modal.msgSuccess("操作成功");
                    var patientId = response.patientId;
                    // console.log("patientId=" + patientId)
                    this.patientForm.patientId = patientId;
                    this.form.patientId = patientId;
                    // console.log(this.form.patientId);
                    // console.log(this.patientForm.patientId)
                    addClinic(this.form).then(response => {
                      this.$modal.msgSuccess("操作成功");
                      // this.open = false;
                      this.getList();
                    });
                    this.open = false;
                  });
                } else {
                  this.$modal.msgError("操作失败，该患者已存在");
                }
              } else {

                let patientId = this.patientForm.patientId;
                getPatient(patientId).then(response => {
                  let patient = response.data;
                  if(patient.patientStatus == '0') {
                    this.$modal.msgError("该患者已在就诊中");
                  } else {
                    if (this.form.clinicId != null) {

                      updateClinic(this.form).then(response => {

                        updatePatient(this.patientForm).then(response => {
                          // console.log("123");
                        });
                        this.$modal.msgSuccess("操作成功");
                        // console.log("123");
                        this.getList();
                      });
                      this.open = false;
                    } else {
                      addClinic(this.form).then(response => {
                        this.$modal.msgSuccess("操作成功");
                        // this.open = false;
                        this.getList();
                      });
                      this.open = false;
                    }
                  }
                });
                // console.log(this.form.patientId);
                // console.log(this.patientForm.patientId)
              }

            }
          })
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clinicIds = row.clinicId || this.ids;
      this.$modal.confirm('是否确认删除就诊信息编号为"' + clinicIds + '"的数据项，以及相关的任务数据？删除后无法恢复！')
        .then(function () {
          return delClinic(clinicIds);
        }).then(() => {
        this.getListbyone();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/clinic/export', {
        ...this.queryParams
      }, `clinic_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
<style>
</style>
